<?php
class Detail extends MY_Controller
{
	public function __construct()
	{
		log_message('debug', '=====开始调用Report->Detail->__construct接口=====');
		parent::__construct();
		$this->loadLanguageFile('application/controllers/Report/');
		log_message('debug', '=====调用Report->Detail->__construct接口结束=====');
	}
	
	public function showView() {
		$this->load->view('module/Report/detail.htm');
	}
	
	public function getListData() {
		log_message('debug', '=====开始调用Report->Detail->getListData接口=====');
		$this->verifyPrivilege();
		
		try {
			$gameAreaIdString = $this->param->getParam('gameAreaIdString', array('method' => 'string', 'emptyErrorCode' => 201, 'invalidErrorCode' => 1));
			$startDate = $this->param->getParam('startDate', array('method' => 'string', 'emptyErrorCode' => 1, 'invalidErrorCode' => 1), 'post', true);
			$endDate = $this->param->getParam('endDate', array('method' => 'string', 'emptyErrorCode' => 1, 'invalidErrorCode' => 1), 'post', true);
			$reportTypeId = $this->param->getParam('reportTypeId', array('method' => 'int', 'min' => 1, 'max' => 2, 'emptyErrorCode' => 1, 'invalidErrorCode' => 1));
			$page = $this->param->getParam('page', array('method' => 'int', 'min' => 1, 'emptyErrorCode' => 1, 'invalidErrorCode' => 1), 'post', true);
			$rows = $this->param->getParam('rows', array('method' => 'int', 'min' => 1, 'emptyErrorCode' => 1, 'invalidErrorCode' => 1), 'post', true);
			
			switch ($reportTypeId) {
				case 1:
					$tableName = 't_data_hourly_report';
					break;
					
				case 2:
					$tableName = 't_data_daily_report';
					break;
			}
			
			$sql = "
				select
					<selectColumns>
				from
					`{$tableName}` a
				left join
					`t_data_game_area` b
				on
					a.`gameAreaId` = b.`gameAreaId`
				left join
					`t_data_operator` c
				on
					b.`operatorId` = c.`operatorId`
				left join
					`t_data_user_operator_privilege` d
				on
					b.`operatorId` = d.`operatorId`
					and
					d.`userId` = '{$this->currUserId}'
				where
					a.`gameAreaId` in ({$gameAreaIdString})
					and
					d.`privilegeValue` = 1
			";
			
			if($startDate) {
				$sql .= "
					and
					a.`datetime` >= '{$startDate} 00:00:00'
				";
			}
			
			if($endDate) {
				$sql .= "
					and
					a.`datetime` <= '{$endDate} 23:59:59'
				";
			}
			
			$sql .= "
				order by
					c.`operatorId`,
					a.`gameAreaId`,
					a.`datetime`
			";
			
			$getTotalSql = str_replace('<selectColumns>', "count(*)", $sql);
			$this->returnData['data']['total'] = $this->gmDatabase->getColumn($getTotalSql);
			$getDataSql = str_replace('<selectColumns>', "a.`datetime`, c.`operatorName`, b.`areaNum`, a.`registerAccountNum`, a.`registerIpAddressNum`, 
                                        a.`createRoleNum`, a.`maxOnlineRoleNum`, a.`avgOnlineRoleNum`, a.`loginAccountNum`, a.`loginIpAddressNum`, 
                                        a.`totalPay`, a.`totalPayAccountNum`,a.`totalRegisterNum`,a.`totalCreateRoleNum`,
                                        a.`payMoney`, a.`payAccountNum`,a.`payRateNum`,a.`arpu`,a.`arppu`,
                                        a.`payMoneyNew`, a.`payAccountNumNew`,a.`payRateNumNew`,a.`arpuNew`,a.`arppuNew`,
                                        a.`payMoneyOld`, a.`payAccountNumOld`,a.`payRateNumOld`,a.`arpuOld`,a.`arppuOld`, 
                                        a.`monthCardNum`, a.`superCardNum`, a.`bothCardNum`", $sql);
				
			if($page && $rows) {
				$startRow = ($page - 1) * $rows;
				$getDataSql .= " limit {$startRow}, {$rows}";
			}
				
			$this->returnData['data']['rows'] = $this->gmDatabase->getArray($getDataSql);
		}
		
		catch(MY_Param_Exception $e) {
			$this->setErrorCode($e->getCode());
		}
		
		catch(MY_Mysql_Exception $e) {
			$this->setErrorCode(1);
		}
		
		catch(Exception $e) {
			$this->setErrorCode($e->getCode());
		}
		
		$this->returnJson();
		log_message('debug', '=====调用Report->Detail->getListData接口结束=====');
	}
}